package chs;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that when Atom is walling, always moving forward in the maximum direction
 * and visits all four corners.
 * 
 * Credit to TestDaCruzerMovement.java, author: Phillip Johnson 
 * for corner visiting tests. 
 * @author Sean Chung
 */
public class TestAtomWalling extends RobotTestBed {
  // Check that Atom's heading is always to the front when walling
  boolean straightHeading;
  // Check that Atom is always moving in the maximum length of battlefield
  boolean maxLength;
  /** True if the robot visited this corner during the test case. */
  boolean visitedUpperLeft = false;
  /** True if the robot visited this corner during the test case. */
  boolean visitedUpperRight = false;
  /** True if the robot visited this corner during the test case. */
  boolean visitedLowerLeft = false;
  /** True if the robot visited this corner during the test case. */
  boolean visitedLowerRight = false;
  

  /**
   * Specifies that SittingDuck and Atom are to be matched up in this test case.
   *
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,chs.Atom";
  }

  /**
   * This test runs for 10 round.
   *
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Check that robot is always facing arena walls, moving maximum amount, visited all corners.
   *
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {    
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    int turn = event.getTurnSnapshot().getTurn();
    // Width of robot from the center is always 40 by default
    final double robotWidth = 40.0;
    double xPos = robot.getX();
    double yPos = robot.getY();
    double velocity = robot.getVelocity();
    // By default the battlefield created is 800 x 600
    int expectedVelocity = (800 / turn);
    
    if (velocity == expectedVelocity) {
      maxLength = true;      
    }  
    if ((xPos < robotWidth) && (yPos < robotWidth)) {
      visitedUpperLeft = true;
    }
    if ((xPos < robotWidth && (yPos > (height - robotWidth)))) {
      visitedLowerLeft = true;
    }
    if ((xPos > (width - robotWidth)) && (yPos < robotWidth)) {
      visitedUpperRight = true;
    }
    if ((xPos > (width - robotWidth) && (yPos > (height - robotWidth)))) {
      visitedLowerRight = true;
    }
    // Check that robots heading is always straight
    if ((robot.getBodyHeading() % 90) == 0) {
      straightHeading = true;
    }
    
    else {
      straightHeading = false;
    }
  }
  
  
  /**
   * Returns a comma or space separated list like: x1,y1,heading1, x2,y2,heading2, which are the 
   * coordinates and heading of robot #1 and #2. So "0,0,180, 50,80,270" means that robot #1 
   * has position (0,0) and heading 180, and robot #2 has position (50,80) and heading 270.
   * 
   * Override this method to explicitly specify the initial positions for your test cases. 
   * 
   * Defaults to null, which means that the initial positions are determined randomly.  Since
   * battles are deterministic by default, the initial positions are randomly chosen but will
   * always be the same each time you run the test case. 
   * 
   * @return The list of initial positions.
   */
  @Override
  public String getInitialPositions() {
    return "300,300,90, 0,0,0";
  }

  /**
   * After the battle, check to see that Atom was walling the entire time.
   *
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Straight Heading", straightHeading);
    assertTrue("Max Length", maxLength);
    assertTrue("Check UpperLeft", visitedUpperLeft);
    assertTrue("Check LowerLeft", visitedLowerLeft);
    assertTrue("Check UpperRight", visitedUpperRight);
    assertTrue("Check LowerRight", visitedLowerRight);
  }
}
